module AboutYou
  module SDK
    ###
    # The Criteria namespace
    ###
    module Criteria
      ###
      # This class is used for the selection of certain product fields
      # which should be fetched in addition when making an api call
      #
      # author:: Collins GmbH & Co KG
      ###
      class ProductFields
        # id and name is set per default
        # product field "is_active"
        IS_ACTIVE              = 'active'
        # product field "brand_id"
        BRAND                  = 'brand_id'
        # product field "description_long"
        DESCRIPTION_LONG       = 'description_long'
        # product field "description_short"
        DESCRIPTION_SHORT      = 'description_short'
        # product field "default_variant"
        DEFAULT_VARIANT        = 'default_variant'
        # product field "variants"
        VARIANTS               = 'variants'
        # product field "min_price"
        MIN_PRICE              = 'min_price'
        # product field "max_price"
        MAX_PRICE              = 'max_price'
        # product field "sale"
        IS_SALE                = 'sale'
        # product field "default_image"
        DEFAULT_IMAGE          = 'default_image'
        # product field "attributes_merged"
        ATTRIBUTES_MERGED      = 'attributes_merged'
        # product field "categories"
        CATEGORIES             = 'categories'
        # product field "inactive_variants"
        INACTIVE_VARIANTS      = 'inactive_variants'
        # product field "max_savings"
        MAX_SAVINGS            = 'max_savings'
        # product field "max_savings_percentage"
        MAX_SAVINGS_PERCENTAGE = 'max_savings_percentage'
        # product field "tags"
        TAGS                   = 'tags'

        ###
        # filters a given set of fields
        #
        # * *Args*    :
        #   - +fields+ -> an Array of product fields which should be filtered
        #
        # * *Returns* :
        #   - a filtered Array of product fields
        ###
        def self.filter_fields(fields)
          fields = fields.uniq

          # this simplifies parsing on (pre)fetching facets
          fields.push(ATTRIBUTES_MERGED) if
          fields.include?(ATTRIBUTES_MERGED) &&
          (fields.include?(BRAND)  ||
          fields.include?(VARIANTS) ||
          fields.include?(DEFAULT_VARIANT))

          fields
        end

        ###
        # determines whether the api call has to require Facets or not
        #
        # * *Args*    :
        #   - +fields+ -> an Array of product fields which should be filtered
        #
        # * *Returns* :
        #   - a boolean which is true when ap call has to require facets
        ###
        def self.requires_facets(fields)
          ([BRAND,
            VARIANTS,
            DEFAULT_VARIANT,
            ATTRIBUTES_MERGED
           ] & fields).count > 0
        end

        ###
        # determines whether the api call has to require Categories or not
        #
        # * *Args*    :
        #   - +fields+ -> an Array of product fields which should be filtered
        #
        # * *Returns* :
        #   - a boolean which is true when ap call has to require categories
        ###
        def self.requires_categories(fields)
          fields.include?(CATEGORIES)
        end
      end
    end
  end
end
